

import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:test_flutter/common/net/address.dart';
import 'package:test_flutter/common/style/common_style.dart';

/// 动态页面
class DynamicPage extends StatefulWidget{
  @override
  State<StatefulWidget> createState() {
    return _DynamicPage();
  }
}

class _DynamicPage extends State<DynamicPage>{

  List<DynamicBean> _data = List.empty(growable: true);

  @override
  void initState() {
    super.initState();
    /// 请求数据
    _getData(1);
  }

  @override
  Widget build(BuildContext context) {
    return Material(
      color: UIColors.common_color_white2,
      child: ListView.separated(itemBuilder: (context, index) {
        var dynamicBean = _data[index];
        String id = dynamicBean.id;
        String avatar = dynamicBean.actor.avatar_url;
        String name = dynamicBean.actor.login;
        var time = dynamicBean.created_at;
        String repoName = dynamicBean.repo.name;
        String tip = dynamicBean.comment.body;
        return Padding(
          padding: EdgeInsets.all(10),
          child: TextButton(
            onPressed: () => print("click $id"),
            style: ButtonStyle(
              backgroundColor: MaterialStateProperty.all(UIColors.common_color_white)
            ),
            child: Container(
              margin: EdgeInsets.only(top: 6, bottom: 6),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Flex(
                    direction: Axis.horizontal,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: <Widget>[
                      ClipOval(
                        child: Image.network(avatar, width: 32, height: 32,),
                      ),
                      Expanded(
                        flex: 1,
                        child: Container(
                          margin: EdgeInsets.only(left: 4, right: 4),
                          child: Text('$name', style: TextStyle(color: UIColors.common_color_black, fontWeight: FontWeight.bold, fontSize: 12),),
                        ),
                      ),
                      Text(time, style: TextStyle(color: UIColors.common_color_black1, fontSize: 14),)
                    ],
                  ),
                  Padding(
                    padding: EdgeInsets.only(top: 10),
                    child: Text("$repoName", style: TextStyle(color: UIColors.common_color_black, fontWeight: FontWeight.bold, fontSize: 12),),
                  ),
                  Visibility( /// 可以控制View显示与隐藏,Offstage也有此作用
                    visible: tip != null && tip.isNotEmpty,
                    child: Padding(
                      padding: EdgeInsets.only(top: 6),
                      child: Text('$tip', style: TextStyle(color: UIColors.common_color_black2, fontSize: 14),),
                    ),
                  ),
                ],
              ),
            ),
          ),
        );
      },
        itemCount: _data.length,
        separatorBuilder: (context, index){ /// 添加分割线
          return Container(
            width: double.infinity,
            height: 2,
          );
        },
      ),
    );
  }

  _getData(int page){
    RepoBean repoBean = RepoBean();
    repoBean.name = 'CZGithubAppFlutter';
    repoBean.id = 123;
    ActorBean actorBean = ActorBean();
    actorBean.id = 987;
    actorBean.avatar_url = 'https://dss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=1361850766,1428296927&fm=26&gp=0.jpg';
    actorBean.login = "czhah";
    CommentBean commentBean = CommentBean();
    commentBean.id = 123456;
    commentBean.node_id = "456789";
    commentBean.body = "树莓派4编译遇到同样的问题，不知该如何解决 ";
    DynamicBean dynamicBean = DynamicBean();
    dynamicBean.id = "123456";
    dynamicBean.created_at = "2020-05-29T03:14:05Z";
    dynamicBean.actor = actorBean;
    dynamicBean.repo = repoBean;
    dynamicBean.comment = commentBean;
    _data.add(dynamicBean);
    _data.add(dynamicBean);
    _data.add(dynamicBean);
    _data.add(dynamicBean);
    _data.add(dynamicBean);
    _data.add(dynamicBean);
    _data.add(dynamicBean);
  }

//  _getData(int page) async{
//    //  加载数据
//    BaseOptions options = BaseOptions(
//      method: "get",
//      baseUrl: Address.HOST,
//      connectTimeout: 12000,
//      sendTimeout: 12000,
//      receiveTimeout: 12000,
//    );
//    Dio dio = Dio(options);
//    dio.interceptors.add(LogInterceptor());
//    try{
//      var response = await dio.post("users/czhah/received_events?page=$page&per_page=20");
//      var jsonStr = jsonDecode("$response");
//      List list = json.decode(jsonStr);
//      List<DynamicBean> temp = List();
//      for(var item in list){
//        var dynamicBean = DynamicBean.fromJson(item);
//        temp.add(dynamicBean);
//      }
//      setState(() {
//        _data.addAll(temp);
//      });
//    }catch(e) {
//      print(e);
//    }
//  }

}

class DynamicBean{
  String id = "";
  String created_at = "";
  ActorBean actor = ActorBean();
  RepoBean repo = RepoBean();
  CommentBean comment = CommentBean();

  DynamicBean();

  DynamicBean.fromJson(Map<String, dynamic> json)
      : id = json['id'], created_at = json['created_at'],
        actor = ActorBean.fromJson(json['avatar_url']),
        repo = RepoBean.fromJson(json['repo']),
        comment = CommentBean.fromJson(json['comment']);

}

class ActorBean{
  int id = 0;
  String login = "";
  String avatar_url = "";
  String url = "";

  ActorBean();

  ActorBean.fromJson(Map<String, dynamic> json)
      : id = json['id'], login = json['login'], avatar_url = json['avatar_url'], url = json['url'];

}

class RepoBean{
  int id = 0;
  String name = "";
  String url = "";

  RepoBean();

  RepoBean.fromJson(Map<String, dynamic> json)
      : id = json['id'], name = json['name'], url = json['url'];

}

class CommentBean{
  int id = 0;
  String node_id = "";
  String body = "";

  CommentBean();

  CommentBean.fromJson(Map<String, dynamic> json)
      : id = json['id'], node_id = json['node_id'], body = json['body'];

}